<?php
/**
 * class Table implement basic CRUD action
 * 
 */
class Table extends Object
{
    //name of table's fields
    protected  $_fieldList = null;
    /**
     * 
     */
    protected  $_rules = null;
    /**
     * vd : $_rules = array(
     *                  'pk' => array('id',..),
     *                  'pk_auto_increment' => TRUE
     *                  'onInsert' => array(''),
     *                  'onSelect' => '*',
     *
     *                      )
     */

    protected $_input_data = null;
    protected $_query_condition;
    
    
    function  __construct()
    {
        $table = get_called_class();
        $this->_rules['tableName'] = substr($table,0,strlen($table) - 5);
    }

    public function insert($dataArray = NULL,$useDefaultPKRule = TRUE)
    {        
        if(is_null($dataArray))
        {
            if(is_null($this->_input_data))
                exit('No insertion data found !');
            $dataArray =& $this->_input_data;
        }
        $db = DataProvider::getInstance();
        if($useDefaultPKRule == TRUE && $this->_rules['pk_auto_increment'] == TRUE)
        {
            $arr = array();
            foreach($dataArray as $key => $value)
            {
                if(!in_array($key,$this->_rules['pk']))
                {
                    $arr[$key] = $value;
                }
            }
            $db->insert($this->_rules['tableName'],$arr);
        }
        else
        {
            $db->insert($this->_rules['tableName'],$dataArray);
        }
        
    }

    public function get($condition = NULL,$optionArray = NULL)
    {
        $db = DataProvider::getInstance();
//        if(is_null($condition))
//        {
//            $condition = $this->_query_condition;
//        }
        if(!isset($optionArray['order']))
            $optionArray['order'] = NULL;
        if(!isset($optionArray['limit']))
            $optionArray['limit'] = 1000;
        if(!isset($optionArray['offset']))
            $optionArray['offset'] = 0;
        return $db->get($this->_rules['tableName'],$condition,$optionArray['order'],$optionArray['limit'],$optionArray['offset']);
    }

    public function update($dataArray,$condition = NULL)
    {
        if(is_null($dataArray))
        {
            if(is_null($this->_input_data))
                exit('No update data found !');
            $dataArray =& $this->_input_data;
        }
        $db = DataProvider::getInstance();
//        if(is_null($condition))
//        {
//            $condition = $this->_query_condition;
//        }
        $db->update($this->_rules['tableName'],$dataArray,$condition);
    }

    public function remove($condition = NULL)
    {
//        if(is_null($condition))
//        {
//            $condition = $this->_query_condition;
//        }
        $db = DataProvider::getInstance();
        $db->remove($this->_rules['tableName'],$condition);
    }

    //getter & setter
    public function setField(&$arrayOfField)
    {
        $this->_fieldList =& $arrayOfField;
    }

    public function setRules(&$rulesArray)
    {
        $tbName = $this->_rules['tableName'];
        $this->_rules =& $rulesArray;
        $this->_rules['tableName'] = $tbName;
    }

    public function setInput($fieldName,$data)
    {
        $this->_input_data[$fieldName] = $data;
    }

    public function refreshInput()
    {
        $this->_input_data = array();
        $this->_query_condition = array();
    }

    public function getFields()
    {
        return $this->_fieldList;
    }

    public function setCondition(&$conditionArray)
    {
        $this->_query_condition =& $conditionArray;
    }

    public function check()
    {
        if(sizeof($this->_fieldList) == 0)
                exit("Table has to have at least 1 field !");
    }
}

?>
